//
//  ComposeViewController.swift
//  我的微博
//
//  Created by Creator on 15/10/20.
//  Copyright © 2015年 Creator. All rights reserved.
//

import UIKit

class ComposeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }

    
    // MARK: - 界面的搭建 
    override func loadView() {
        view = UIView()
        
        // 视图控制器一定要设置背景颜色
        view.backgroundColor = UIColor.whiteColor()

        perpareNav()
        
        perpareToolbar()
        
        perpareTextView()
        
    }
    
    
    // MARK: - 准备文本框
    private func perpareTextView() {
    
        view.addSubview(textView)
        
        textView.backgroundColor = UIColor.whiteColor()
        
        // 自动布局
        // 提示：在 iOS 开发中，如果是单纯的 nav + scrollView 会自动调整滚动视图的边距
        // 会将顶部的导航栏的高度预留出来，就能实现滚动视图穿透效果
        textView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
        textView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: view, size: nil)
        textView.ff_AlignVertical(type: ff_AlignType.TopRight, referView: toolbar, size: nil)
        
        // 添加占位图
        placeholderLabel.text = "分享新鲜事..."
        placeholderLabel.sizeToFit()
        textView.addSubview(placeholderLabel)
        placeholderLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 5, y: 8))
        
        
    }
    
    // MARK: - toolbar 的准备
    private func perpareToolbar() {
        
        // 颜色美工会告诉我们的
        toolbar.backgroundColor = UIColor(white: 0.5, alpha: 1.0)
        view.addSubview(toolbar)
        
        // 自动布局
        toolbar.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: view, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44))
        
        // 定义一个数组
        let itemSettings = [["imageName": "compose_toolbar_picture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        
        var items = [UIBarButtonItem]()
        
        for dict in itemSettings {
        
            
            // 遍历构造函数
            items.append(UIBarButtonItem(imageName: dict["imageName"]!, target: self, action: dict["action"]))
        
            
            // 弹簧
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
            
        }
        
        // 删除最后一个 item
        items.removeLast()
        
        // 设置 toolbar 的 item
        toolbar.items = items
    }
    
    

    // MARK: - 导航栏
    private func perpareNav() {
    
        // 左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        
        // 禁用发布按钮
        navigationItem.rightBarButtonItem?.enabled = false
        
        // 标题栏
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 32))
        let titleLabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        titleLabel.text = "发微博"
        titleView.addSubview(titleLabel)
        titleLabel.ff_AlignInner(type: ff_AlignType.TopCenter, referView: titleView, size: nil)
        
        let nameLabel = UILabel(color: UIColor.redColor(), fontSize: 13)
        nameLabel.text = UserAccount.sharedAccount?.name ?? ""
        nameLabel.sizeToFit()
        titleView.addSubview(nameLabel)
        nameLabel.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: titleView, size: nil)
        
        navigationItem.titleView = titleView
        
        
    }
    
    // MARK: 监听方法
    func close() {
        dismissViewControllerAnimated(true, completion: nil)

    }
    
    func sendStatus() {
    
    }
    
    func inputEmoticon() {
        print("哈哈哈哈哈哈啊")
    }
    
    
    // MARK: - 懒加载
    
    ///  文本视图
    private lazy var textView: UITextView = {
    
        let tv = UITextView()
        
        tv.font = UIFont.systemFontOfSize(18)
        
        // 允许垂直拖拽
        tv.alwaysBounceVertical = true
        
        // 拖拽时关闭键盘
        tv.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        

        return tv
        
    }()
    
    ///  工具栏
    private lazy var toolbar = UIToolbar()
    
    ///  占位标签
    private lazy var placeholderLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 18)
    
    

}
